using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_JXC_StockOrderSale : Member, IRequiresSessionState
{
	protected ScriptManager ScriptManager1;
	protected Literal ltrTitle;
	protected TextBox txtStockNumber;
	protected TextBox txtInputTime;
	protected TextBox txtInputer;
	protected TextBox txtReceiptEndDate;
	protected Label lblBuyerName;
	protected Label lblPayTime;
	protected Label lblDeliver_TotalPrice;
	protected Label lblInvoice_TotalPrice;
	protected Label lblMoneyGoods;
	protected Label lblReceiptMoney;
	protected Label lblAreadyReceiptMoney;
	protected Label lblFee;
	protected TextBox txtRemarks;
	protected HiddenField hfProductStr;
	protected HiddenField hfOrderID;
	protected HiddenField hfMoneyGoods;
	protected HiddenField hfDeliver_TotalPrice;
	protected HiddenField hfInvoice_TotalPrice;
	protected HiddenField hfFee;
	protected HiddenField hfMoney;
	protected HiddenField hfAlreadyMoney;
	protected HiddenField hfTime;
	protected HiddenField hfDeliverStatus;
	protected HtmlGenericControl divProduct;
	protected Button btnSave;
	protected Button btnCancle;
	private int infoOrderID = 0;
	private int infoID = 0;
	private Shop_Product _shop_product = null;
	private bool ownerpower = false;
	private string actionInfo = "";
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public int InfoOrderID
	{
		get
		{
			if (base.Request.QueryString["infoOrderID"] != null)
			{
				this.infoOrderID = Convert.ToInt32(base.Request.QueryString["infoOrderID"].Trim());
			}
			return this.infoOrderID;
		}
		set
		{
			this.infoOrderID = value;
		}
	}
	public int InfoID
	{
		get
		{
			if (this.InfoOrderID > 0)
			{
				DataTable dataTable = BLLInstance.BllAccountAoucher.GetList("StockOrderID=" + this.InfoOrderID).Tables[0];
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					this.infoID = Convert.ToInt32(dataTable.Rows[i]["AccountAoucherID"].ToString());
				}
			}
			return this.infoID;
		}
	}
	protected Shop_Product shop_product
	{
		get
		{
			Shop_Product shop_product;
			if (this._shop_product == null)
			{
				this._shop_product = new Shop_Product(this.conn);
				shop_product = this._shop_product;
			}
			else
			{
				shop_product = this._shop_product;
			}
			return shop_product;
		}
	}
	public bool Ownerpower
	{
		get
		{
			if (base.OwnerPowerFinancemanage(base.user.UserID))
			{
				this.ownerpower = true;
			}
			return this.ownerpower;
		}
	}
	public string ActionInfo
	{
		get
		{
			this.actionInfo = base.ActionTabInfo;
			return this.actionInfo;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			string actionTabInfo = base.ActionTabInfo;
			if (actionTabInfo != null)
			{
				if (!(actionTabInfo == "addinfo"))
				{
					if (actionTabInfo == "editinfo")
					{
						this.ltrTitle.Text = "修改销售出库单";
						this.btnSave.Text = "修改";
					}
				}
				else
				{
					this.txtStockNumber.Text = "XSCKD" + DateTime.Now.TimeString();
					this.txtInputTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
					this.txtReceiptEndDate.Text = DateTime.Now.AddDays(1.0).ToString("yyyy-MM-dd HH:mm:ss");
					this.txtInputer.Text = base.user.UserName;
					this.ltrTitle.Text = "添加销售出库单";
				}
			}
			if (this.InfoOrderID > 0)
			{
				this.InitPage();
			}
		}
	}
	private bool CheckInput()
	{
		bool result = true;
		if (!string.IsNullOrEmpty(this.txtInputTime.Text))
		{
			if (!PageValidate.IsDateTime(this.txtInputTime.Text))
			{
				MessageBox.Show(this, "记录时间格式错误！");
				result = false;
			}
		}
		if (!string.IsNullOrEmpty(this.txtReceiptEndDate.Text))
		{
			if (!PageValidate.IsDateTime(this.txtReceiptEndDate.Text))
			{
				MessageBox.Show(this, "收款期限格式错误！");
				result = false;
			}
		}
		return result;
	}
	protected void InitPage()
	{
		HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(this.InfoOrderID);
		if (model != null)
		{
			string actionTabInfo = base.ActionTabInfo;
			if (actionTabInfo != null)
			{
				if (actionTabInfo == "editinfo")
				{
					this.txtStockNumber.Text = model.InfoNumber;
					this.txtInputTime.Text = model.CreateDate.ToString();
					this.txtInputTime.Enabled = false;
					this.txtInputer.Text = model.Inputer;
					this.txtReceiptEndDate.Text = model.ReceiptEndDate.ToString();
					this.txtRemarks.Text = model.Remark;
					this.hfOrderID.Value = model.OrderID.ToString();
				}
			}
		}
	}
	private HoWave.Data.Model.StockInfoOrder GetStockInfoOrder()
	{
		HoWave.Data.Model.StockInfoOrder stockInfoOrder = new HoWave.Data.Model.StockInfoOrder();
		string text = this.txtStockNumber.Text;
		DateTime value = DateTime.Parse(this.txtInputTime.Text);
		string text2 = this.txtRemarks.Text;
		decimal num = 0m;
		if (!string.IsNullOrEmpty(this.hfFee.Value))
		{
			num = Convert.ToDecimal(this.hfFee.Value);
		}
		decimal num2 = num;
		if (!string.IsNullOrEmpty(this.hfMoney.Value))
		{
			decimal d = Convert.ToDecimal(this.hfMoney.Value);
			num2 += d;
		}
		decimal value2 = 0m;
		if (!string.IsNullOrEmpty(this.hfAlreadyMoney.Value))
		{
			value2 = Convert.ToDecimal(this.hfAlreadyMoney.Value);
		}
		DateTime value3 = DateTime.Parse(this.txtReceiptEndDate.Text);
		if (!string.IsNullOrEmpty(this.hfTime.Value))
		{
			DateTime value4 = Convert.ToDateTime(this.hfTime.Value);
			stockInfoOrder.ReceiptDate = new DateTime?(value4);
		}
		if (!string.IsNullOrEmpty(this.hfOrderID.Value))
		{
			stockInfoOrder.OrderID = new int?(Convert.ToInt32(this.hfOrderID.Value));
		}
		stockInfoOrder.StockStatus = new int?(2);
		if (!string.IsNullOrEmpty(this.hfDeliverStatus.Value))
		{
			if (this.hfDeliverStatus.Value == "1" || this.hfDeliverStatus.Value == "2")
			{
				stockInfoOrder.StockStatus = new int?(3);
				stockInfoOrder.StockDate = new DateTime?(DateTime.Now);
			}
		}
		stockInfoOrder.InfoNumber = text;
		stockInfoOrder.CreateDate = new DateTime?(value);
		stockInfoOrder.Remark = text2;
		stockInfoOrder.StockOrderType = "outSell";
		stockInfoOrder.Fee = new decimal?(num);
		stockInfoOrder.ReceiptMoney = new decimal?(num2);
		stockInfoOrder.AlreadyReceiptMoney = new decimal?(value2);
		stockInfoOrder.ReceiptEndDate = new DateTime?(value3);
		stockInfoOrder.Inputer = this.txtInputer.Text;
		stockInfoOrder.OwnerID = base.user.UserID;
		return stockInfoOrder;
	}
	private int AddAccountAoucher(int stockOrderID, string infoNumber)
	{
		int result = 0;
		HoWave.Data.Model.AccountAoucher accountAoucher = new HoWave.Data.Model.AccountAoucher();
		accountAoucher.StockOrderID = new int?(stockOrderID);
		accountAoucher.AccountANumber = "JZPZ" + DateTime.Now.TimeString();
		accountAoucher.AccountACount = new int?(1);
		accountAoucher.AccountAExplain = "编号" + infoNumber + "的出库单记账凭证";
		accountAoucher.Inputer = base.user.UserName;
		accountAoucher.IsCaudit = true;
		accountAoucher.CauditName = base.user.UserName;
		accountAoucher.CauditDate = new DateTime?(DateTime.Now);
		accountAoucher.IsAccount = true;
		accountAoucher.AccountName = base.user.UserName;
		accountAoucher.AccountDate = new DateTime?(DateTime.Now);
		accountAoucher.InputDate = new DateTime?(DateTime.Now);
		accountAoucher.OwnerID = base.user.UserID;
		int num = BLLInstance.BllAccountAoucher.Add(accountAoucher);
		if (num > 0)
		{
			result = this.AAIAddBySell(num);
		}
		return result;
	}
	private int AAIAddBySell(int aainfoID)
	{
		int result = 0;
		decimal d = 0m;
		if (!string.IsNullOrEmpty(this.hfMoney.Value))
		{
			d = Convert.ToDecimal(this.hfMoney.Value);
		}
		decimal moneyGoods = 0m;
		if (!string.IsNullOrEmpty(this.hfMoneyGoods.Value))
		{
			moneyGoods = Convert.ToDecimal(this.hfMoneyGoods.Value);
		}
		decimal deliver_TotalPrice = 0m;
		if (!string.IsNullOrEmpty(this.hfDeliver_TotalPrice.Value))
		{
			deliver_TotalPrice = Convert.ToDecimal(this.hfDeliver_TotalPrice.Value);
		}
		decimal invoice_TotalPrice = 0m;
		if (!string.IsNullOrEmpty(this.hfInvoice_TotalPrice.Value))
		{
			invoice_TotalPrice = Convert.ToDecimal(this.hfInvoice_TotalPrice.Value);
		}
		decimal num = 0m;
		if (!string.IsNullOrEmpty(this.hfFee.Value))
		{
			num = Convert.ToDecimal(this.hfFee.Value);
		}
		decimal num2 = d + num;
		string actionTabInfo = base.ActionTabInfo;
		if (actionTabInfo != null)
		{
			if (!(actionTabInfo == "addinfo"))
			{
				if (actionTabInfo == "editinfo")
				{
					if (num > 0m)
					{
						bool flag = false;
						bool flag2 = false;
						DataTable dataTable = BLLInstance.BllAccountAoucherItem.GetList("AccountAoucherID=" + aainfoID).Tables[0];
						for (int i = 0; i < dataTable.Rows.Count; i++)
						{
							int accountAItemID = Convert.ToInt32(dataTable.Rows[i]["AccountAItemID"].ToString());
							int accountSubjectID = Convert.ToInt32(dataTable.Rows[i]["AAItemAccountSID"].ToString());
							HoWave.Data.Model.AccountAoucherItem model = BLLInstance.BllAccountAoucherItem.GetModel(accountAItemID);
							if (model != null)
							{
								model.AccountAItemID = accountAItemID;
								HoWave.Data.Model.AccountSubject model2 = BLLInstance.BllAccountSubject.GetModel(accountSubjectID);
								if (model2 != null)
								{
									string a = model2.AccountSName.Trim();
									if (a == "应收账款")
									{
										model.BorrowPrice = new decimal?(num2);
										BLLInstance.BllAccountAoucherItem.Update(model);
										flag2 = true;
									}
									else
									{
										if (a == "手续费")
										{
											model.LoanPrice = new decimal?(num);
											BLLInstance.BllAccountAoucherItem.Update(model);
											flag = true;
										}
										else
										{
											if (a == "商品销售收入" || a == "运输费" || a == "发票费")
											{
												flag2 = true;
											}
										}
									}
								}
							}
						}
						if (flag2 && !flag)
						{
							BLLInstance.BllAccountAoucherItem.AddAccountAoucherItem(aainfoID, 0m, num, "销售出库单", "手续费");
						}
					}
				}
			}
			else
			{
				result = BLLInstance.BllAccountAoucherItem.AccountAoucherItemAddBySell(aainfoID, num2, moneyGoods, deliver_TotalPrice, invoice_TotalPrice, num);
			}
		}
		return result;
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			HoWave.Data.Model.StockInfoOrder stockInfoOrder = this.GetStockInfoOrder();
			string actionTabInfo = base.ActionTabInfo;
			if (actionTabInfo != null)
			{
				if (!(actionTabInfo == "addinfo"))
				{
					if (actionTabInfo == "editinfo")
					{
						stockInfoOrder.InfoOrderID = this.InfoOrderID;
						stockInfoOrder.UpdateDate = new DateTime?(DateTime.Now);
						bool flag = BLLInstance.BllStockInfoOrder.Update(stockInfoOrder);
						if (flag)
						{
							this.WriteSuccessMsg("", "修改销售出库单信息成功！", "StockOrderSaleSearch.aspx");
						}
					}
				}
				else
				{
					int num = BLLInstance.BllStockInfoOrder.Add(stockInfoOrder);
					if (num > 0)
					{
						int num2 = BLLInstance.BllStockItemOrder.AddStockOrderItem(num, this.txtStockNumber.Text, this.hfProductStr.Value);
						if (num2 > 0)
						{
							this.WriteSuccessMsg("", "添加销售出库单信息成功！", "StockOrderSaleSearch.aspx");
						}
					}
					else
					{
						this.WriteErrMsg("", "添加销售出库单信息失败！", "");
					}
				}
			}
		}
	}
	public void btnCancle_Click(object sender, EventArgs e)
	{
		base.Response.Redirect("StockOrderSaleSearch.aspx");
	}
}
